<template>
  <div class="m-life">
    <el-row>
      <el-col :span="14">
        <slider :adList="adList" />
      </el-col>
      <el-col :span="4">
        <div
          class="m-life-pic"
          :style="{ background: 'url(' + pic + ')' }"
          @click="detail('水果')"
        />
      </el-col>
      <el-col :span="6">
        <div class="m-life-login">
          <h4>
            <img :src="avatar" alt="" />
          </h4>

          <template v-if="username && token">
            <p class="m-life-login-name">Hi！你好 {{ username }}</p>
            <p>
              <nuxt-link to="/userCenter"
                ><el-button round size="medium">个人中心</el-button></nuxt-link
              >
            </p>
            <p>
              <el-button round size="medium" @click="logout">退出</el-button>
            </p>
          </template>
          <template v-else>
            <p class="m-life-login-name">Hi！你好</p>
            <p>
              <nuxt-link to="/register"
                ><el-button round size="medium">注册</el-button></nuxt-link
              >
            </p>
            <p>
              <nuxt-link to="/login"
                ><el-button round size="medium">立即登录</el-button></nuxt-link
              >
            </p>
          </template>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="7">
        <div
          class="m-life-fresh"
          :style="{ background: 'url(' + fresh + ')' }"
          @click="detail('海鲜')"
        />
      </el-col>
      <el-col :span="7">
        <div
          class="m-life-drink"
          :style="{ background: 'url(' + drink + ')' }"
          @click="detail('肉类')"
        />
      </el-col>
      <el-col :span="4">
        <div
          class="m-life-veg"
          :style="{ background: 'url(' + veg + ')' }"
          @click="detail('蔬菜')"
        />
      </el-col>
      <el-col :span="6">
        <div class="m-life-downapp">
          <img
            src="//s1.meituan.net/bs/fe-web-meituan/60ac9a0/img/download-qr.png"
            alt="小程序"
          />
          <p>乐鲜小程序版即将上架</p>
          <h4>
            <span class="red">1元起</span><em class="gary">生鲜送到家</em>
          </h4>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import Slider from "./slider.vue";
export default {
  components: {
    Slider
  },
  data() {
    return {
      adList: [],
      pic: require("@/assets/img/0855a9ee3fe4eab2.jpg"),
      fresh: require("@/assets/img/20201206171656.png"),
      drink: require("@/assets/img/20201206172713.png"),
      veg: require("@/assets/img/20201206180127.png")
    };
  },
  mounted() {
    if (this.$store.state.user.username && this.$store.state.user.token) {
      this.$store.dispatch("GetInfo");
    }
  },
  computed: {
    username() {
      return this.$store.state.user.username;
    },
    token() {
      return this.$store.state.user.token;
    },
    avatar() {
      const avatar = this.$store.state.user.avatar;
      console.log(typeof avatar);
      console.log(avatar);
      return avatar === "" ? require("@/assets/img/avator.png") : avatar;
    }
  },
  props: {
    advertiseList: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  watch: {
    advertiseList() {
      this.adList = this.advertiseList;
    }
  },
  methods: {
    detail(key) {
      this.$router.push({
        path: "/products",
        query: {
          keyword: key
        }
      });
    },
    logout() {
      this.$router.push({ path: "/login" });
    }
  }
};
</script>

<style lang="scss">
@import "@/assets/css/index/life.scss";
</style>
